<template>
	<view>
		<!-- 积分支付 -->
		<view class="mainpadding_top" v-if="type==3">
			<view class="margin_top flexcolumn">
				<view class="titletext hei_text bold">实付</view>
				<view class="margin_top6 hei_text bold" style="font-size: 40rpx;">积分{{pay_price}}+运费{{freight_price}}
				</view>
			</view>
			<view class="mainpadding_top flexbetween margin_top6 ffffff radius">
				<view class="flexleft">
					<image class="iconclass margin_right3" src="@/static/image/system/score.png" mode=""></image>
					<view class="titletext hei_text">积分（可用{{userInfo.score}}）</view>
				</view>
				<u-icon name="checkmark-circle-fill" color="#ff8505" size="24"></u-icon>
			</view>
			<view class="mainpadding_top  margin_top6 ffffff radius">
				<view class="flexbetween" @click="pay_type=10">
					<view class="flexleft">
						<u-icon name="weixin-circle-fill" color="#26db6f" size="28"></u-icon>
						<view class="titletext hei_text margin_left">微信</view>
					</view>
					<u-icon name="checkmark-circle-fill" v-if="pay_type==10" color="#ff8505" size="24"></u-icon>
					<u-icon name="checkmark-circle-fill" v-if="pay_type!=10" color="#999" size="24"></u-icon>
				</view>
				<!-- #ifdef APP-PLUS -->
				<view class="margin_top5 flexbetween" @click="pay_type=20">
					<view class="flexleft">
						<u-icon name="zhifubao-circle-fill" color="#55aaff" size="28"></u-icon>
						<view class="titletext hei_text margin_left">支付宝</view>
					</view>
					<u-icon name="checkmark-circle-fill" v-if="pay_type==20" color="#ff8505" size="24"></u-icon>
					<u-icon name="checkmark-circle-fill" v-if="pay_type!=20" color="#999" size="24"></u-icon>
				</view>
				<!-- #endif -->
			</view>
		</view>
		<!-- 保证金缴纳 -->
		<view class="mainpadding_top" v-if="type==1 || type==2 || type==4">
			<view class="margin_top flexcolumn">
				<view class="titletext hei_text bold">实付金额</view>
				<view class="margin_top6 hei_text bold" style="font-size: 40rpx;">{{pay_price}}</view>
			</view>
			<view class="mainpadding_top  margin_top6 ffffff radius">
				<view class="flexbetween" @click="pay_type=10">
					<view class="flexleft">
						<u-icon name="weixin-circle-fill" color="#26db6f" size="28"></u-icon>
						<view class="titletext hei_text margin_left">微信</view>
					</view>
					<u-icon name="checkmark-circle-fill" v-if="pay_type==10" color="#ff8505" size="24"></u-icon>
					<u-icon name="checkmark-circle-fill" v-if="pay_type!=10" color="#999" size="24"></u-icon>
				</view>
				<!-- #ifdef APP-PLUS -->
				<view class="margin_top5 flexbetween" @click="pay_type=20">
					<view class="flexleft">
						<u-icon name="zhifubao-circle-fill" color="#55aaff" size="28"></u-icon>
						<view class="titletext hei_text margin_left">支付宝</view>
					</view>
					<u-icon name="checkmark-circle-fill" v-if="pay_type==20" color="#ff8505" size="24"></u-icon>
					<u-icon name="checkmark-circle-fill" v-if="pay_type!=20" color="#999" size="24"></u-icon>
				</view>
				<!-- #endif -->
			</view>

		</view>
		<view class="mainpadding gudingdb">
			<view class="bigbtn" @click="jifenPay()">立即支付</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				pay_price: "",
				type: 1, //1=大礼包,2=普通商品,3=积分商品
				userInfo: {},
				order_id: "",
				pay_type: 10,
				freight_price: "",
				openid: ""
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.msg))
			this.pay_price = data.pay_price
			this.type = data.type
			this.order_id = data.order_id
			if (this.type == 3) {
				this.freight_price = data.freight_price
				this.init()
			}
		},
		onShow() {
			// #ifdef MP-WEIXIN
			this.getOpenid()
			// #endif
		},
		methods: {
			getOpenid() {
				let _this = this
				uni.login({
					success(res) {
						if (res.code) {
							uni.showLoading({
								mask: true,
								title: "请稍后"
							})
							httpRequest.request('/api/user/getopenid', 'GET', {
								code: res.code
							}).then(resp => {
								uni.hideLoading()
								_this.openid = resp.data.openid
							}).catch(err => {})
						}
					}
				})
			},
			init() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.userInfo = res.data
				})
			},
			jifenPay() {
				if (this.type == 3) {
					if (Number(this.userInfo.score) < Number(this.pay_price)) {
						httpRequest.toast("可用积分不足，无法兑换")
						return false
					}
				}
				// 订单来源:10=app,20=微信小程序
				let platform = 10
				// #ifdef MP-WEIXIN
				platform = 20
				// #endif
				httpRequest.request('/api/order/payment', 'POST', {
					pay_type: this.pay_type,
					order_id: this.order_id,
					platform,
					openid: this.openid,
				}).then(res => {
					if (res.code == 1) {
						if (this.pay_type == 10) { //微信
							if (platform == 10) {
								uni.requestPayment({
									provider: 'wxpay',
									orderInfo: res.data.appconfig, //后台返回的支付数据
									success: function(res) {
										uni.navigateBack(1)
									},
									fail: function(err) {
										console.log(err)
									}
								});
							} else {
								uni.requestPayment({
									provider: 'wxpay',
									timeStamp: res.data.wxconfig.timeStamp,
									nonceStr: res.data.wxconfig.nonceStr,
									package: res.data.wxconfig.package,
									signType: res.data.wxconfig.signType,
									paySign: res.data.wxconfig.paySign,
									success: function(resf) {
										// 支付成功
										uni.navigateBack(1)
									},
									fail: function(err) {
										// 支付失败

									}
								});
							}
						} else { //支付宝
							uni.requestPayment({
								provider: 'alipay',
								orderInfo: res.data.appconfig, //后台返回的支付数据
								success: function(res) {
									uni.navigateBack(1)
								},
								fail: function(err) {
									console.log(err)
								}
							});
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.iconclass {
		width: 40rpx;
		height: 40rpx;
	}
</style>